define(function () {
  /**
   * 线条标绘
   * @param {Cesium.Viewer} viewer 基本视图
   * @param {Cesium.ScreenSpaceEventHandler} handler 屏幕监听事件
   * @param {Cesium.CustomDataSource}plotEntities 标绘实体集合
   */
  let polylinePlotting = function (viewer, handler, plotEntities) {
    let activeShapePoints = []                       //线段端点集合

    /**
     *添加线实体
     *@param {Cesium.Cartesian3 []} positionData 线段数组
     */
    function drawShape(positionData) {
      console.log(positionData)
      return shape = plotEntities.entities.add({
        description: 'plot',
        polyline: {
          positions: positionData,
          width: 3,
        },
      })
    }

    /**
     *添加点
     *@param {Cesium.Cartesian3} worldPosition
     */
    function createPoint(worldPosition) {
      return plotEntities.entities.add({
        description: 'plot',
        position: worldPosition,
        point: {
          color: Cesium.Color.WHITE,
          pixelSize: 5,
        },
      })
    }

    //左键点击插入数组
    handler.setInputAction((event) => {
      let earthPosition = viewer.camera.pickEllipsoid(event.position, viewer.scene.globe.ellipsoid)  //笛卡尔坐标

      if (activeShapePoints.length === 0) {
        createPoint(earthPosition)
      }
      activeShapePoints.push(earthPosition)

      //截取端点数组后两位，再传参绘画
      drawShape(activeShapePoints.slice(-2))
      createPoint(earthPosition)
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK)

    //右键点击，初始化
    handler.setInputAction(function (event) {
      activeShapePoints = []
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
  }
  return polylinePlotting
})